<?php
class URLHandler extends CBaseURLHandler
{

	protected $localroutes;
	public function __construct($config=null)
	{
		parent::__construct($config);
		$this->localroutes=array();	
	}
	
	public function init($config=null)
	{
		parent::init($config);
		if(is_file(dirname(__FILE__).'/URLHandler.cfg.php'))
		{
			$this->localroutes=include(dirname(__FILE__).'/URLHandler.cfg.php');		
		}
	}
	
	public function findRoute($requestURI)
	{
		
		foreach ($this->localroutes as $localroute)
		{
			$url_pattern=$localroute['url_pattern'];
			$moduleid=$localroute['moduleid'];
			$componentId=$localroute['componentId'];
			$controller=$localroute['controller'];
			$method=$localroute['method'];
			if(isset($localroute['theme']))
			$theme=$localroute['theme'];
			$interceptors=$localroute['interceptors'];
			preg_match($url_pattern, $requestURI, $matches);
			if(isset($matches[0]))
			{
				$defaultr= $this->routes->itemAt('default-'.$moduleid);
				$defaultr->setRouteIdentifier($url_pattern);
				$defaultr->setComponentId($componentId);
				$defaultr->setController($controller);
				if(isset($theme))
				$defaultr->setTheme($theme);
				$defaultr->setControllerPath($defaultr->getModuleBasePath().
							DIRECTORY_SEPARATOR.'components'.
							DIRECTORY_SEPARATOR.$componentId.
							DIRECTORY_SEPARATOR.'controllers');
				preg_match('#P\<(.*?)\>#', $method, $match);
				if(isset($match[1]))
				$method=$matches[$match[1]];
				$defaultr->setMethod($method);
				$defaultr->setInterceptors($interceptors);
				$_REQUEST = array_merge($_REQUEST, $matches);
                $_GET     = array_merge($_GET    , $matches);
				return $defaultr;
			} 			
		
		}
		//var_dump($this->localroutes);
		return parent::findRoute($requestURI);
	}
	
	
}